/*SelectModeRejected.java
 * 
 *  Shared Preferences code based on example from http://developer.android.com/guide/topics/data/data-storage.html
 *  Alert Dialog code based on example from http://developer.android.com/guide/topics/ui/dialogs.html
 * 
 */
package com.tagit;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SelectModeRejected extends Activity {
	public static final String PREFS_NAME = "MyPrefsFile";

	private GridView gridview;
	private PhotoCursorAdapter mCursorAdapter;
	Button btUnreject;
	private PhotosDbAdapter mPhotoDb;
	SharedPreferences settings;
	int numAlgos;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		settings = getSharedPreferences(PREFS_NAME, 0);

		mPhotoDb = new PhotosDbAdapter(this);
		mPhotoDb.open();
		mCursorAdapter = MyApp.getAdapter();
		setContentView(R.layout.selectmoderejected);

		gridview = (GridView) findViewById(R.id.gridview2);
		mCursorAdapter.setState(1);
		gridview.setAdapter(mCursorAdapter);
		registerForContextMenu(gridview);
		setPicListener();
		setDoneClickListener();
		// setRejectClickListener();
		btUnreject = (Button) findViewById(R.id.unreject);

		// Set listener for "Unreject" button
		btUnreject.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (mCursorAdapter.getSelected().size() < 1) {
					Context context = getApplicationContext();
					CharSequence text = "Please select at least one picture before unrejecting";
					int duration = Toast.LENGTH_LONG;
					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
				} else {
					AlertDialog.Builder builder = createAlertDialog();
					AlertDialog alert = builder.create();
					alert.show();
				}

			}
		});
		gridview.setAdapter(mCursorAdapter);

		// Count number of buttons set algorithmically
		numAlgos = 0;
	}

	// Set listener for photo thumbnails
	public void setPicListener() {
		gridview.setOnItemClickListener(new OnItemClickListener() {
			// @Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				if (!(mCursorAdapter.isSelected(mCursorAdapter
						.getItemId(position)))) {
					mCursorAdapter.addSelected(mCursorAdapter
							.getItemId(position));
					gridview.setAdapter(mCursorAdapter);
				} else {
					mCursorAdapter.removeSelected(mCursorAdapter
							.getItemId(position));
					gridview.setAdapter(mCursorAdapter);
				}
			}
		});
	}

	// Method for when a user clicks on Done button
	public void setDoneClickListener() {
		Button doneButton = (Button) findViewById(R.id.done);
		doneButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(v.getContext(),
						PhotoGalleryRejected.class);
				startActivity(intent);
			}
		});
	}

	// Method to create alert dialog to confirm user wants to "unreject" a photo
	private AlertDialog.Builder createAlertDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to unreject this pictures?")
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								int sizeunrejected = mCursorAdapter
										.getSelected().size();
								mPhotoDb.unrejectMultiplePhotos(mCursorAdapter
										.getSelected());
								mCursorAdapter = new PhotoCursorAdapter(
										SelectModeRejected.this, mPhotoDb
												.selectRejected(20), 1);
								gridview.setAdapter(mCursorAdapter);

								// Toast message to let know the user he
								// unrejected X number of photos
								Context context = getApplicationContext();
								CharSequence text = "You just unrejected "
										+ sizeunrejected + " pictures";
								int duration = Toast.LENGTH_LONG;
								Toast toast = Toast.makeText(context, text,
										duration);
								toast.show();
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		// AlertDialog alert = builder.create();
		return builder;
	}

	private void CreateMenu(Menu menu) {
		menu.setQwertyMode(true);
		MenuItem mnu1 = menu.add(0, 0, 0, "Preferences");
		{
			mnu1.setAlphabeticShortcut('a');
		}
		MenuItem mnu2 = menu.add(0, 1, 1, "Help");
		{
			mnu2.setAlphabeticShortcut('b');
		}
	}

	private boolean MenuChoice(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			goToPreferences();
			return true;
		case 1:
			goToHelp();
			return true;
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		CreateMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return MenuChoice(item);
	}

	public void goToPreferences() {
		Intent intent = new Intent(SelectModeRejected.this, Preferences.class);
		startActivity(intent);
	}

	public void goToHelp() {
		Intent intent = new Intent(SelectModeRejected.this, Help.class);
		startActivity(intent);
	}

}
